import re
import time

from myreaper import *


def get_fxname(tr, i):
    s = re.sub('VSTi: ', 'VST:', RPR_TrackFX_GetFXName(tr, i, '', 128)[3])
    return re.sub(' \\(.*\\)', '', s)


def get_track_params(tr):
    lst = []
    n_fx = RPR_TrackFX_GetCount(tr)
    for i in range(n_fx):
        id_ = 0
        param_name = '?'
        params = [get_fxname(tr, i).replace(' ', '_')]
        while param_name:
            print(RPR_TrackFX_GetParamName(tr, i, id_, '', 64))
            param_name = RPR_TrackFX_GetParamName(tr, i, id_, '', 64)[4]
            param_value = RPR_TrackFX_GetParamNormalized(tr, i, id_)
            params.append(str(param_value))
            id_ += 1
        lst.append(','.join(params))
    return ';'.join(lst)


if __name__ == '__main__':
    fx_names = []
    track_params = []
    t0 = time.time()

    dir_ = 'C:/REAPER/Scripts/presets/Sylenth1'
    presets = [
        'Ultrasonic_-_CHORDS_-_Arrow.fxp',
        'Ultrasonic_-_CHORDS_-_Bamboo.fxp',
        'Ultrasonic_-_CHORDS_-_Fatality.fxp',
    ]

    print('正在等待初始数据……')
    r = ''
    clear_recv(0)

    while r == '':
        cmd = "RPR_GetLastTouchedTrack()"
        clear_send(0)
        send(0, cmd)
        r = recv(0)

    print(r)
    print('收到初始数据，开始处理。')

    tr = RPR_GetLastTouchedTrack()
    n_fxs = RPR_TrackFX_GetCount(tr)

    print(get_track_params(tr))

    '''while True:
        delta_t = time.time() - t0
        y = 0.5 + 0.5 * math.sin(delta_t)

        print(n_fxs)

        time.sleep(1)'''

